package com.qf.rbac2202.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qf.rbac2202.sys.entity.*;
import com.qf.rbac2202.sys.mapper.SysRoleMapper;
import com.qf.rbac2202.sys.service.ISysDeptService;
import com.qf.rbac2202.sys.service.ISysRoleDeptService;
import com.qf.rbac2202.sys.service.ISysRoleMenuService;
import com.qf.rbac2202.sys.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.rbac2202.utils.PageQueryUtils;
import com.qf.rbac2202.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author dong_
 * @since 2022-10-28
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl< SysRoleMapper, SysRole > implements ISysRoleService {

	@Autowired
	ISysDeptService iSysDeptService;

	@Autowired
	ISysRoleMenuService iSysRoleMenuService;

	@Autowired
	ISysRoleDeptService iSysRoleDeptService;

	@Override
	public PageVo getPageVo( Map< String, Object > params ) {

		IPage< SysRole > page1=PageQueryUtils.getPage( params );

		String role_name=(String) params.get( "roleName" );

		//判断传过来的角色名是否为空
		QueryWrapper< SysRole > sysRoleQueryWrapper=new QueryWrapper<>( );
		sysRoleQueryWrapper.eq( StringUtils.isNotBlank( role_name ),"role_name",role_name );


		this.page( page1,sysRoleQueryWrapper );

		List< SysRole > records=page1.getRecords( );
		//查询所有的部门名单
		List< SysDept > list=iSysDeptService.list( );

		HashMap< Long, String > hashMap=new HashMap<>( );

		list.forEach( s->hashMap.put( s.getDeptId( ),s.getName( ) ) );


		records.forEach( s->s.setDeptName( hashMap.get( s.getDeptId( ) ) ) );

		return new PageVo( page1 );
	}

	/**
	 * 保存信息
	 *
	 * @param sysRole
	 */
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void saveRole( SysRole sysRole ) {
		//保存角色基本信息
		sysRole.setCreateTime( new Date( ) );

		this.save( sysRole );

		//获取主键回填产生的id
		Long roleId=sysRole.getRoleId( );

		//保存角色和菜单的对应关系
		List< Long > menuIdList=sysRole.getMenuIdList( );

		//将菜单对象转为中间表对象
		//可以用两参构造简化代码
		List< SysRoleMenu > sysRoleMenuList=menuIdList.stream( ).map( s->{
					SysRoleMenu sysRoleMenu=new SysRoleMenu( );
					sysRoleMenu.setMenuId( s );
					sysRoleMenu.setRoleId( roleId );
					return sysRoleMenu;
				}
		).collect( Collectors.toList( ) );

		//批量插入数据
		iSysRoleMenuService.saveBatch( sysRoleMenuList );

		//3.保存角色部门表
		List< Long > deptIdList=sysRole.getDeptIdList( );
		List< SysRoleDept > sysRoleDeptList=deptIdList.stream( ).map( s->new SysRoleDept( roleId,s ) )
				.collect( Collectors.toList( ) );

		iSysRoleDeptService.saveBatch( sysRoleDeptList );

	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public SysRole getRoleById( Long roleId ) {


		SysRole roleById=this.getById( roleId );

		//查询菜单id
		QueryWrapper< SysRoleMenu > sysRoleMenuQueryWrapper=new QueryWrapper<>( );
		sysRoleMenuQueryWrapper.eq( "role_id",roleId );
		sysRoleMenuQueryWrapper.select( "menu_id" );
		List< Object > menuIdList=iSysRoleMenuService.listObjs( sysRoleMenuQueryWrapper );
		List< Long > menuIdListStrList=menuIdList.stream( ).map( s->Long.valueOf( s.toString( ) ) ).collect( Collectors.toList( ) );
		roleById.setMenuIdList( menuIdListStrList );


		//查询部门id
		QueryWrapper< SysRoleDept > sysRoleDeptQueryWrapper=new QueryWrapper<>( );
		sysRoleDeptQueryWrapper.eq( "role_id",roleId );
		sysRoleDeptQueryWrapper.select( "dept_id" );
		List< Object > objects=iSysRoleDeptService.listObjs( sysRoleDeptQueryWrapper );
		List< Long > collect=objects.stream( ).map( s->(Long) s ).collect( Collectors.toList( ) );
		roleById.setDeptIdList( collect );

		return roleById;
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteId( @RequestBody List< String > roleIds ) {

		List< Long > roleId=roleIds.stream( ).map( s->Long.valueOf( s ) ).collect( Collectors.toList( ) );

		//删除角色表中有关于角色的所有信息
		QueryWrapper< SysRole > sysRoleQueryWrapper=new QueryWrapper<>( );
		sysRoleQueryWrapper.in( "role_id",roleId );
		this.remove( sysRoleQueryWrapper );

		QueryWrapper< SysRoleDept > sysRoleDeptQueryWrapper=new QueryWrapper<>( );
		QueryWrapper< SysRoleMenu > sysRoleMenuQueryWrapper=new QueryWrapper<>( );
		sysRoleDeptQueryWrapper.in( "role_id",roleId );
		sysRoleMenuQueryWrapper.in( "role_id",roleId );

		iSysRoleMenuService.remove( sysRoleMenuQueryWrapper );
		iSysRoleDeptService.remove( sysRoleDeptQueryWrapper );
		//删除部门表里面所有有关余角色的信息
		//删除中间表中的信息

	}

	@Override
	public void updateRole( SysRole sysRole ) {
		Long roleId=sysRole.getRoleId( );
		List< String > list=new ArrayList<>( );
		list.add( roleId.toString( ) );
		//先删除
		deleteId( list );

		//再添加
		saveRole( sysRole );

	}
}
